<?php
namespace Asset\Controller;
use Common\Controller\AdminbaseController;
class ProviderController extends AdminbaseController {
	protected $qrcodeusers_model;

	function _initialize() {
		parent::_initialize();
		$this->qrcodeusers_model = D("QrcodeUsers");
	}
	function index(){
		$this->_lists();
		$this->display();
	}
	function _lists(){
		$count=$this->qrcodeusers_model->count();
		$page = $this->page($count, 50);
		$qrcodeusers=$this->qrcodeusers_model
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")->select();
    	$this->assign('page', $page->show('Admin'));
		$this->assign("qrcodeusers", $qrcodeusers);
		$this->assign("count", $count);
	}
	function add(){
		$this->display();
	}
	function add_post(){
		if(IS_POST){
			if(!I('post.name') && !I('post.callback')){
				$this->error("请填写调用商信息和回调地址");
			}
			$name = I('post.name');
			$_POST['secret'] = str_replace('%','',urlencode(base64_encode(hash_hmac("sha1", "bbt-$name",C("LOCAL_URL") ,true))));
	        $_POST['create'] = date("Y-m-d H:i:s");
			$_POST['userid'] = get_current_admin_id();
			if($this->qrcodeusers_model->add($_POST)){
				$this->success("添加成功！",U('Provider/index'));
			} else {
				$this->error("添加失败！");
			}
		}
	}
	public function up(){
    	if (I("get.id")) {
    		$rst = $this->qrcodeusers_model->where(array('id'=>I("get.id")))->setField('status','1');
    		if ($rst) {
    			$this->success("密钥启用成功！");
    		} else {
    			$this->error('密钥启用失败！');
    		}
    	} else {
			$this->error($this->qrcodeusers_model->getError());
    	}
    }

    public function down(){
    	if (I("get.id")) {
    		$rst = $this->qrcodeusers_model->where(array('id'=>I("get.id")))->setField('status','2');
    		if ($rst) {
    			$this->success("密钥停用成功！");
    		} else {
    			$this->error('密钥停用失败！');
    		}
    	} else {
			$this->error($this->qrcodeusers_model->getError());
    	}
    }
}
